/**
 * 
 */
package com.vote.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vote.dao.AnswerInfoDao;
import com.vote.entity.AnswerInfo;

//Spring Bean的标识.
@Service
// 默认将类中的所有函数纳入事务管理.
@Transactional
public class AnswerInfoManager{
	
	private static Logger log = LoggerFactory.getLogger(AnswerInfoManager.class);
	@Autowired
	private AnswerInfoDao answerInfoDao;
	
	/**
	 * 批量保存
	 * @param answerLst
	 * @return
	 */
	public boolean batchSave(List<AnswerInfo> answerLst){
		boolean flag=false;
		if(answerLst!=null && answerLst.size()>0){
			for(AnswerInfo answer:answerLst){
				answerInfoDao.save(answer);				
				log.info("ip:" + answer.getId()
						+ " cookie:" + answer.getCookie()
						+ " title:" + ((answer.getQuestionInfo()!=null)?(answer.getQuestionInfo().getTitle()):"")	
						+ " answer:"+((answer.getQuestionOpt()!=null)?(answer.getQuestionOpt().getContent()):""));
			}
			flag=true;
		}		
		return flag;
	}
	
	public Boolean isAnswerQuestion(String ip,String cookie){
		Boolean flag=false;
		if(StringUtils.isEmpty(ip) || StringUtils.isEmpty(cookie)){
			return false;
		}
		if(answerInfoDao.getAnswerInfoCnt(ip, cookie)>0){
			flag=true;
		}
		return flag;
	}
	
}
